Write Web Apps with Dart: Develop and Design by Jack Murphy

Write Web Apps with Dart: Develop and Design by Jack Murphy

Author:Jack Murphy [Murphy, Jack]
Language: eng
Format: epub
Publisher: Pearson Education
Published: 2015-12-08T23:00:00+00:00


Embedding Documents

One of the powerful features of MongoDB is the ability to embed documents. Collections are a great way to create logical groupings, but those separations mean you can’t execute a single query to get data from both collections; you have to issue two separate queries. For a lot of applications, that is perfectly acceptable. However, in cases where performance is a top priority, Mongo offers the capacity to embed documents inside documents.

Embedding allows you to nest objects inside objects. This is a familiar practice to anyone who’s worked with JSON data structures.

Let’s update the collection to use some embedded documents to track game states for each player. First, let’s drop the collection so you can clean up the data. Dropping a collection removes all the documents from a collection.

1. Run the drop() and find() functions:

> db.BaseballStats.drop()

true

> db.BaseballStats.find()

>

Let’s take a look at the JSON structure of the documents you are going to insert.

[

{

"name": "Doe",

"games": [

{

"date": "06-22-2015",

"at_bats": 3,

"hits": 1

},

{

"date": "06-23-2015",

"at_bats": 3,

"hits": 1

},

{

"date": "06-23-2015",

"at_bats": 3,

"hits": 1

}

]

},

{

"name": "Yates",

"games": [

{

"date": "06-22-2015",

"at_bats": 4,

"hits": 2

},

{

"date": "06-23-2015",

"at_bats": 2,

"hits": 0

},

{

"date": "06-23-2015",

"at_bats": 4,

"hits": 4

}

]

}

]

Next, you will insert the above JSON code into your collection. Luckily, the Mongo client supports multiline entries.

2. Construct the opening line of the query, and press Return/Enter.

> db.BaseballStats.insert(

...

You can manually begin entering the JSON code, or you can paste it directly into the command prompt.

3. Enter each new line as needed by pressing the Return/Enter key.

MongoDB understands that you are creating a document. It will wait for the JSON structure to be closed before executing.

4. When you’re finished placing the JSON structure into the MongoDB client, append an additional closing parenthesis, and press Return/Enter.

... )

If you entered the JSON code correctly, Mongo created two new documents for you. Each document contains a field named games that will be an array of nested documents.

5. Run a find() command to take a look at how Mongo has stored your data:

Click here to view code image

> db.BaseballStats.find()

{ "_id" : ObjectId("55953c05a9e58e4c4304eadb"), "name" : "Doe", "games" : [ { "date" : "06-22-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 } ] }

{ "_id" : ObjectId("55953c05a9e58e4c4304eadc"), "name" : "Yates", "games" : [ { "date" : "06-22-2015", "at_bats" : 4, "hits" : 2 }, { "date" : "06-23-2015", "at_bats" : 2, "hits" : 0 }, { "date" : "06-23-2015", "at_bats" : 4, "hits" : 4 } ] }

6. Query a top-level object using the established query convention:

Click here to view code image

> db.BaseballStats.find({name: "Doe"})

{ "_id" : ObjectId("55953c05a9e58e4c4304eadb"), "name" : "Doe", "games" : [ { "date" : "06-22-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 } ] }

7. Ask the question “which players had a game with four hits?” by constructing the following query. It will return the entire root object.

Click here to view code image

> db.BaseballStats.find({"games.hits": 4})

{ "_id" : ObjectId("55953c05a9e58e4c4304eadc"),



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.